package de.homelabs.bookmarkmanager.desktop.qt.view;

import java.net.MalformedURLException;
import java.net.URL;

import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QTextEdit;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

import de.homelabs.bookmarkmanager.persistence.domain.HBookmark;

public class HBookmarkDetailView extends QWidget {
	private QLineEdit title;
	private QLineEdit link;
	private QTextEdit description;
	
	//public members for signals
	public QPushButton buttonSave;
	public QPushButton buttonCancel;
	
	//bookmark
	private HBookmark activeItem = null;
	
	private boolean itemModified = false;
	
	/**
	 * constructor
	 */
	public HBookmarkDetailView(HMainWidget parent){
		//setparent
		super(parent);
		
		QGroupBox groupBox = new QGroupBox();
		QVBoxLayout vbox = new QVBoxLayout();
		QVBoxLayout wVbox = new QVBoxLayout();
		//groupBox.setLayout(vbox);
		//vbox.addWidget(groupBox);
		
		groupBox.setTitle("Link Details");
		
		//Elements
		QLabel lable1= new QLabel();
		lable1.setText("Titel");
		title = new QLineEdit();
		QLabel lable2 = new QLabel("Link");
		link = new QLineEdit();
		QLabel lable3 = new QLabel("Beschreibung");
		description = new QTextEdit();
		
		//buttons
		QHBoxLayout hbox = new QHBoxLayout();
		buttonSave = new QPushButton("speichern");
		buttonSave.setEnabled(false);
		
		buttonCancel = new QPushButton("abbrechen");
		buttonCancel.setEnabled(false);
		hbox.addWidget(buttonSave);
		hbox.addWidget(buttonCancel);
		QWidget buttonWidget = new QWidget();
		buttonWidget.setLayout(hbox);
		
		//fit elements
		vbox.addWidget(lable1);
		vbox.addWidget(title);
		vbox.addWidget(lable2);
		vbox.addWidget(link);
		vbox.addWidget(lable3);
		vbox.addWidget(description);
		vbox.addWidget(buttonWidget);
		vbox.setAlignment(new Qt.Alignment(Qt.AlignmentFlag.AlignTop));
		groupBox.setLayout(vbox);
		wVbox.addWidget(groupBox);
		this.setLayout(wVbox);
		
		//connect signals
		this.buttonCancel.clicked.connect(this, "onCancel()");
		this.buttonSave.clicked.connect(this, "onSave()");
		
	}
	
	/********************
	 * event handling	*
	 ********************/
	protected void onCancel(){
		this.title.setText("");
		this.link.setText("");
		this.description.setText("");
		this.activeItem = null;
		this.itemModified = false;
		
		//disable buttons
		this.buttonSave.setEnabled(false);
		this.buttonCancel.setEnabled(false);
	}
	
	protected void onSave(){
		this.activeItem.setTitle(this.title.text());
		try {
			this.activeItem.setLink(new URL(this.link.text()));
		} catch (MalformedURLException e) {
			System.out.println("kann url nicht erstellen");
		}
		this.activeItem.setTags(this.description.toPlainText());
		HMainWidget parent = (HMainWidget) this.parent();
		parent.messageToStatusbar("Datensatz wurde gespeichert!");
	}
	
	/*******************
	 * getter / setter *
	 *******************/
	public void setViewData(HBookmark bookmark){
		this.title.setText(bookmark.getTitle());
		this.link.setText(bookmark.getLink().toString());
		this.description.setText(bookmark.getTags().toString());
		this.activeItem = bookmark;
		
		//activate buttons
		this.buttonSave.setEnabled(true);
		this.buttonCancel.setEnabled(true);
	}
	
	public HBookmark getActiveItem(){
		return this.activeItem;
	}
	
	public boolean isModified(){
		return itemModified;
	}
}
